package com.gome.ss.webserver.servlet.view;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.gome.ss.common.Utils;
import com.gome.ss.monitor.data.SpiderkitInfoConnection;
import com.gome.ss.monitor.model.GroupSummary;

public class GroupSummaryServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(GroupSummaryServlet.class);
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String zkhost = req.getParameter("zkhost");
		
		if (zkhost == null || zkhost.equals(""))
			zkhost = "127.0.0.1:2181";
		
		req.setAttribute("zkhost", zkhost);
		req.setAttribute("connect_failed", false);
		req.setAttribute("getinfo_failed", false);
		
		SpiderkitInfoConnection skinfoClient = new SpiderkitInfoConnection();
		
		GroupSummary gsummary;

		try {
			skinfoClient.connect(zkhost);
		} catch (Exception e) {
			logger.error(Utils.getTrace(e));
			req.setAttribute("connect_failed", true);
			req.getRequestDispatcher("monitor-views/group.jsp").forward(req, resp);
			return;
		}

		String gname = req.getParameter("gname");
		if (gname != null) {
			skinfoClient.deleteNullGroup(gname);
		}
		
		try {
			skinfoClient.updateFromServer();
			gsummary = skinfoClient.getGroupSummary();
			req.setAttribute("gsummary", gsummary);
		} catch (Exception e) {
			logger.error(Utils.getTrace(e));
			req.setAttribute("getinfo_failed", true);
			req.getRequestDispatcher("monitor-views/group.jsp").forward(req, resp);
			return;
		}
		
		req.getRequestDispatcher("monitor-views/group.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
